package com.controller.gzp;

import com.entity.Role;
import com.service.gzp.RoleService;
import com.util.gzp.AjaxResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * ClassName: RoleController
 * Package: com.controller.gzp
 *
 * @author: guanzepeng
 * @Date: 2021/4/8 15:01 星期四
 * @Description: 角色信息接口
 */
@RestController
@Api(tags = "角色信息接口")
@RequestMapping("/role")
@Validated
@Slf4j
public class RoleController {

	@Autowired
	private RoleService roleServiceImpl;

	/**
	 * 新增的时候查询所有角色
	 * @return 返回数据
	 */
	@PostMapping("/findBy")
	@ApiOperation(value = "新增的时候查询所有角色", response = AjaxResponse.class)
	public AjaxResponse findBy() {
		AjaxResponse by = roleServiceImpl.findBy();
		return by;
	}

	/**
	 * 查询所有的角色
	 * @param rname 传入角色名称 模糊查询
	 * @param rid 传入角色ID 进行单条查询
	 * @param pageNum 传入分页数据
	 * @param pageSize 传入分页数据
	 * @return 返回角色信息
	 */
	@GetMapping("/find")
	@ApiOperation(value = "查询所有的角色", response = AjaxResponse.class)
	public AjaxResponse findByRole(String rname, Integer rid,
	                               @RequestParam(defaultValue = "0") Integer pageNum,
	                               @RequestParam(defaultValue = "1") Integer pageSize) {
		AjaxResponse byRole = roleServiceImpl.findByRole(rname, rid, pageNum, pageSize);
		return byRole;
	}

	/**
	 * 新增一个角色
	 * @param role 传入角色数据
	 * @return 返回成功失败
	 */
	@PostMapping("/insertRole")
	@ApiOperation(value = "新增一个角色", response = AjaxResponse.class)
	public AjaxResponse insertRole(@Valid Role role) {
		System.out.println(role);
		AjaxResponse ajaxResponse = roleServiceImpl.insertRole(role);
		return ajaxResponse;
	}

	/**
	 * 修改一个角色的信息
	 * @param role 传入要修改的信息,用户ID为必填参数
	 * @return 返回成功失败
	 */
	@PostMapping("/updateRole")
	@ApiOperation(value = "修改一个角色的信息", response = AjaxResponse.class)
	public AjaxResponse updateRole(@Valid Role role) {
		AjaxResponse ajaxResponse = roleServiceImpl.updateRole(role);
		return ajaxResponse;
	}

	/**
	 * 删除一个角色
	 * @param rid 传入角色ID
	 * @return 返回成功失败
	 */
	@PostMapping("/deleteRole")
	@ApiOperation(value = "删除一个角色", response = AjaxResponse.class)
	@ApiImplicitParam(name = "rid", value = "传入角色ID", required = true, dataTypeClass = Integer.class)
	public AjaxResponse deleteRole(@NotNull(message = "角色ID不能为空") Integer rid) {
		AjaxResponse ajaxResponse = roleServiceImpl.deleteRole(rid);
		return ajaxResponse;
	}

}
